<%@page import="Connectdata.connectdatabase" %>
<%@page import="java.lang.Math.*" %>
<%@page import="java.text.DecimalFormat"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>



<%
            if (request.getParameter("idsp") != null && request.getParameter("idsp") != "") {

                int idSP = Integer.parseInt(request.getParameter("idsp").toString());
                String name = request.getParameter("name").toString();
                int price = Integer.parseInt(request.getParameter("price").toString());
                if (session.getAttribute("somathang") == null) {
                    session.setAttribute("somathang", 0);
                }
                if (session.getAttribute("demsorow") == null) {
                    session.setAttribute("demsorow", 0);
                }
                int somathang = Integer.parseInt(session.getAttribute("somathang").toString());
                int demsorow = Integer.parseInt(session.getAttribute("demsorow").toString());
                int flag = 0;
                int temp_gh = 1;
                while (temp_gh <= somathang) {

                    if (session.getAttribute("idSP" + temp_gh) != null) {
                        if (session.getAttribute("idSP" + temp_gh).toString().equals(request.getParameter("idsp").toString())) {
                            flag = 1;
                            session.setAttribute("temp", temp_gh);
                            break;
                        }
                    }
                    temp_gh++;
                }

                if (flag == 0) {
                    somathang++;
                    demsorow++;
                    session.setAttribute("somathang", somathang);
                    session.setAttribute("demsorow", demsorow);
                    session.setAttribute("idSP" + somathang, idSP);
                    session.setAttribute("name" + somathang, name);
                    session.setAttribute("price" + somathang, price);
                    session.setAttribute("soluong" + somathang, 1);
                } else {
                    if (Integer.parseInt(session.getAttribute("soluong" + session.getAttribute("temp")).toString()) < 9) {
                        session.setAttribute("soluong" + session.getAttribute("temp").toString(), Integer.parseInt(session.getAttribute("soluong" + session.getAttribute("temp").toString()).toString()) + 1);
                    }
                }
            }

%>


<%

            int demsorow = 0;
            int somathang = 0;
            if (session.getAttribute("demsorow") != null) {
                demsorow = Integer.parseInt((String) session.getAttribute("demsorow").toString());
            }
            if (session.getAttribute("somathang") != null) {
                somathang = Integer.parseInt((String) session.getAttribute("somathang").toString());
            }
            if (session.getAttribute("somathang") != null && demsorow > 0) {
                double total = 0;
%>
<table style="width: 200px;color: white" border="1">
    <tr>
        <td style="font-weight: bold" >TenSP</td>
        <td style="font-weight: bold" >Soluong</td>
    </tr>
    <%
        for (int temp_i = 1; temp_i <= somathang; temp_i++) {
    %>
    <tr>
        <%
            if (session.getAttribute("idSP" + temp_i) != null) {

        %>
        <td><% out.print(session.getAttribute("name" + temp_i).toString());%></td>
        <td><% out.print((String) session.getAttribute("soluong" + temp_i).toString());%></td>
        <%
                if (total <= 2500000) {
                    total = total + Integer.parseInt(session.getAttribute("soluong" + temp_i).toString()) * Integer.parseInt(session.getAttribute("price" + temp_i).toString());
                    session.setAttribute("checktotal", "ok");
                }
                if (total > 2500000) {
                    session.setAttribute("checktotal", "fail");
                }
            }
        %>
    </tr>
    <%}%>
</table><br/>
<a style="cursor: pointer;" onclick="LoadGioHang()">Payment</a>
<%
            }

%>